home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / go2.arc / LISTING1.C < prev    next >
Encoding:
Text File  |  1989-07-21  |  4.0 KB  |  175 lines

  1. /* This version is for Microsoft C 5.1
  2.  * and Quick C 2.0
  3.  *
  4.  * Compilation syntax:
  5.  *    cl -W3 -Ox go.c -F 1000
  6.  * or
  7.  *    qcl -W3 -Ox go.c -F 1000
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <dos.h>
  13. #include <direct.h>
  14. #include <string.h>
  15.  
  16. #ifndef TRUE
  17.     #define TRUE  1
  18.     #define FALSE 0
  19. #endif
  20.  
  21.  
  22.  
  23. int   file_search = TRUE;
  24. int   dir_search  = TRUE;
  25. int   orig_drive;
  26. char *orig_dir;
  27. int   search_drive;
  28. char *search_string;
  29. char *dir_search_string;
  30. int   dir_search_length;
  31. char *file_search_string;
  32. int   reset_drive = FALSE;
  33. int   reset_dir   = FALSE;
  34.  
  35. void do_resets(void);
  36. void help(void);
  37. void test_dir(void);
  38.  
  39. void main(int argc, char **argv)
  40. {
  41.     unsigned new_drive, total_drives;
  42.  
  43.     atexit(do_resets);
  44.  
  45.     if (argc < 2 || argc > 3)
  46.         help();
  47.  
  48.     _dos_getdrive(&orig_drive);
  49.     orig_dir = getcwd(NULL,0);
  50.  
  51.     if (argc == 3)
  52.         if (strcmpi(argv[2],"-F") == 0 || strcmpi(argv[2],"/F") == 0)
  53.             dir_search  = FALSE;
  54.         else
  55.             help();
  56.  
  57.     search_string = strdup(strupr(argv[1]));
  58.     if(search_string[1] == ':')
  59.         {
  60.         new_drive = search_string[0] - 'A' + 1;
  61.         _dos_setdrive(new_drive, &total_drives);
  62.         reset_drive = TRUE;
  63.         search_string += 2;
  64.         }
  65.  
  66.     if (search_string[0] == '\\')
  67.         {
  68.         search_string++;
  69.         file_search = FALSE;
  70.         }
  71.  
  72.     if(strchr(search_string,'?') || strchr(search_string,'*'))
  73.         dir_search = FALSE;
  74.  
  75.     if (dir_search)
  76.         {
  77.         dir_search_string = search_string;
  78.         dir_search_length = strlen(dir_search_string);
  79.         }
  80.  
  81.     if (file_search)
  82.         {
  83.         file_search_string = malloc(strlen(search_string)+3);
  84.         strcpy(file_search_string, search_string);
  85.         if(! strchr(file_search_string, '.' ))
  86.             strcat(file_search_string, ".*");
  87.         }
  88.  
  89.     if (dir_search | file_search)
  90.         {
  91.         chdir("\\");
  92.         test_dir();
  93.         reset_dir = TRUE;
  94.         exit(0);
  95.         }
  96.     else
  97.         help();
  98. }
  99.  
  100. void help(void)
  101. {
  102.     puts("\nGO moves you quickly from one subdirectory to another.");
  103.     puts("Syntax:");
  104.     puts("      GO [d:][\\]pathname [-F]");
  105.     puts("         the pathname can be either the name of a directory or");
  106.     puts("         the name of a file.  It may contain wild cards.\n");
  107.     puts("         If 'd:' is included, drive 'd:' will be used instead");
  108.     puts("         of the current drive.\n");
  109.     puts("         If '\\' is included at the beginning of the pathname,");
  110.     puts("         only subdirectory names will be searched.\n");
  111.     puts("         If '-F' or /F' is included, or if the pathname includes");
  112.     puts("         wild card sybmols, only file names will be searched\n");
  113.     puts("         Normally, both file names and subdirectory names are");
  114.     puts("         searched to match the specified pathname.");
  115.     exit(0);
  116. }
  117.  
  118. void do_resets(void)
  119. {
  120.     unsigned total_drives;
  121.  
  122.     if(reset_dir)
  123.         {
  124.         chdir(orig_dir);
  125.         puts ("   Requested subdirectory not found\a");
  126.         }
  127.     else
  128.         {
  129.         fputs("   New Directory: ",stdout);
  130.         puts (getcwd(NULL,0));
  131.         }
  132.     
  133.     if(reset_drive)
  134.         _dos_setdrive(orig_drive,&total_drives);
  135. }
  136.  
  137. void test_dir(void)
  138. {
  139.     char  *current_dir, *cptr;
  140.     int    i;
  141.     struct find_t find_buffer;
  142.  
  143.     current_dir = getcwd(NULL,0);
  144.     if(dir_search && ((i = strlen(current_dir)) >= dir_search_length))
  145.         {
  146.         cptr = current_dir + (i - dir_search_length);
  147.         if (!strcmp(cptr,search_string))
  148.             exit(0);
  149.         }
  150.  
  151.     if(file_search)
  152.         {
  153.         if(! _dos_findfirst(file_search_string,_A_NORMAL,&find_buffer))
  154.             exit(0);
  155.         }
  156.  
  157.     _dos_findfirst("*.*", _A_SUBDIR, &find_buffer);
  158.     if(find_buffer.attrib == _A_SUBDIR && find_buffer.name[0] != '.')
  159.         {
  160.         chdir(find_buffer.name);
  161.         test_dir();
  162.         chdir(current_dir);
  163.         }
  164.  
  165.     while (! _dos_findnext(&find_buffer))
  166.         if(find_buffer.attrib == _A_SUBDIR && find_buffer.name[0] != '.')
  167.             {
  168.             chdir(find_buffer.name);
  169.             test_dir();
  170.             chdir(current_dir);
  171.             }
  172.  
  173.     free(current_dir);
  174. }